package com.lp.batch;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.context.support.GenericXmlApplicationContext;

import com.lp.batch.domain.Node;
import com.lp.batch.domain.Taxonomies;
import com.lp.batch.domain.Taxonomy;
import com.lp.batch.service.TaxonomyService;
import com.lp.batch.service.impl.TaxonomyServiceImpl;

/**
 * This class is intended to produce large Taxonomy.xml
 * 
 * @author Zainul Franciscus
 *
 */
public class TaxonomyWriter {

	/**
	 * Write a taxonomy xml file with 20.000 nodes, and each node has 10 child nodes
	 * 
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
		ctx.load("classpath:test-context.xml");
		ctx.refresh();

		TaxonomyService taxonomyService = ctx.getBean("taxonomyService", TaxonomyServiceImpl.class);

		Taxonomies taxonomies = new Taxonomies();
		Taxonomy taxonomy = new Taxonomy();
		taxonomy.setName("world");

		for (int i = 0; i < 20000; i++) {
			Node n = new Node();
			n.setAtlasNodeId(i);
			n.setName("City " + i);

			n.addChildNode(create10ChildNodes());

			taxonomy.addChildNode(n);
		}

		taxonomies.setTaxonomy(taxonomy);
		taxonomyService.writeToXML(taxonomies, "out.xml");
	}

	/**
	 * @return a node that has 10 child nodes.
	 */
	private static Node create10ChildNodes() {
		
		
		List<Node> nodes = new ArrayList<Node>();
		for (int i = 0; i < 10; i++) {
			Node n = new Node();
			n.setName("ChildNode" + i);
			nodes.add(n);

			if (nodes.size() > 1) {
				nodes.get(i - 1).addChildNode(n);
			}
		}

		return nodes.get(0);

	}

}
